<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Http\Request;
use Validator;
class Category extends Model
{
    use SoftDeletes;
    /**
     * 关联到模型的数据表
     *
     * @var string
     */
    protected $table = 'categorys';

    protected $primaryKey = 'id';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'title',
        'parentid',
        'path'
    ];

    /**
     * 软删除(回收站功能)
     * @var array
     */
    protected $dates = ['deleted_at'];

    /**
     * 添加分类(增)
     * @param Request $request
     * @param $parent_id
     * @return array
     */
    public function add(Request $request,$parent_id)
    {
        $validator = Validator::make($request->all(), [
            'title' => 'required|unique:categorys',
        ],[
            'title.required' => '分类名不能为空',
            'title.unique' =>'分类名已存在',
        ]);
        if($validator->fails()) {
            $msg = $validator->errors()->all();
            return ['status' => -1, 'error' => $msg];
        }
        $data = $request->only(['title']);

        $category = $this;
        $cate = $category->where('id',$parent_id)->first();
        $category->title = $data['title'];

        if($parent_id){
            $category->parentid = $parent_id;
            $category->path = $cate->path.','.$parent_id;
        }else {
            $category->parentid = $parent_id;
            $category->path = $parent_id;
        }
        if($category->save())
            return ['status' => 1, 'msg' => '添加分类成功'];
        else
            return ['status' => 0, 'msg' => '添加分类失败'];
    }

    /**
     * 软删除(放入回收站回收站)(删)
     * @param $id
     * @return array
     */
    public function cateDelete($id)
    {
        try{
            $result = Category::lists('parentid')->search($id);
            if($result)
                return ['status' => '0','msg' => '父类不能删除'];
            if(Category::where('id',$id)->delete())
                return ['status' => 1, 'msg' => '删除成功'];
            else
                return ['status' => '0','msg' => '删除失败'];
        }catch (\Exception $exception){
            return ['status' => '0','msg' => '删除失败'];
        }
    }

    /**
     * 修改分类名称(改)
     * @param Request $request
     * @return array
     */
    public function edit(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'title' => 'required',
        ],[
            'title.required' => '用户名不能为空',
        ]);
        if($validator->fails()){
            $msg = $validator->errors()->all();
            return ['status' => -1, 'error' => $msg];
        }

        $data = $request->only(['title']);

        $category = $this->where('id', $request->get('id'))->first();

        try{
            $category->update($data);
            return ['status' => 1, 'msg' => '修改成功'];
        }catch (\Exception $exception){
            return ['status' => 0, 'msg' => '分类名已经存在'];
        }
    }
}
